package com.phantasy.modules.npc
{
	import com.phantasy.common.GlobalObjectManager;
	import com.phantasy.scene.WorldManager;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	public class NPCDataManager 
	{
		
		public static const NPC_BASE_ID_INDEX:int = 0;
		public static const NPC_BASE_NAME_INDEX:int = 1;
		public static const NPC_BASE_JOB_INDEX:int = 2;
		public static const NPC_BASE_ACTION_INDEX:int = 3;
		public static const NPC_BASE_AVATAR_INDEX:int = 4;
		public static const NPC_BASE_SKIN_INDEX:int = 5;
		public static const NPC_BASE_CONTENT_INDEX:int = 6;
		public static const NPC_BASE_TYPE_INDEX:int = 7;
		public static const NPC_BASE_MAP_ID_INDEX:int = 8;
		public static const NPC_BASE_ICON_INDEX:int = 9;
		public static const NPC_BASE_CAN_SEARCH_WAY:int = 10;
		public static const NPC_BASE_SAYWORD:int = 11;
		public static const NPC_JOB_ID_INDEX:int = 0;
		public static const NPC_JOB_NAME_INDEX:int = 1;
		public static const NPC_ACTION_ID_INDEX:int = 0;
		public static const NPC_ACTION_NAME_INDEX:int = 1;
		public static const NPC_ACTION_CONDITION_INDEX:int = 2;
		public static const NPC_DEFAULT_ID:int = 0;
		public static const NPC_DEFAULT_JOB_ID:int = 0;
		public static const NPC_DEFAULT_ACTION_ID:int = 0;
		
		private static var instance:NPCDataManager;
		
		private var _npcBaseDataObj:Dictionary;
		private var _npcActionObj:Dictionary;
		private var _npcJobObj:Dictionary;
		private var _npcPos:Dictionary;
		
		public function NPCDataManager()
		{
			this._npcBaseDataObj = new Dictionary();
			this._npcActionObj = new Dictionary();
			this._npcJobObj = new Dictionary();
			this._npcPos = new Dictionary();
		}
		
		public static function getInstance():NPCDataManager
		{
			if (instance == null)
			{
				instance = new (NPCDataManager)();
			};
			return (instance);
		}
		
		
		public function initNpcData(_arg1:ByteArray):void
		{
			var _local5:Array;
			var _local6:Array;
			var _local7:Array;
			_arg1.uncompress();
			var _local2:Array = _arg1.readObject();
			var _local3:Array = _arg1.readObject();
			var _local4:Array = _arg1.readObject();
			for each (_local5 in _local2)
			{
				this._npcBaseDataObj[_local5[NPC_BASE_ID_INDEX]] = _local5;
			}
			for each (_local6 in _local3)
			{
				this._npcActionObj[_local6[NPC_ACTION_ID_INDEX]] = _local6;
			}
			for each (_local7 in _local4)
			{
				this._npcJobObj[_local7[NPC_JOB_ID_INDEX]] = _local7;
			}
		}
		
		public function setPos(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void
		{
			this._npcPos[_arg1] = [_arg2, _arg3, _arg4];
		}
		
		public function getPos(_arg1):Array
		{
			return (this._npcPos[_arg1]);
		}
		
		public function getNpcInfo(_arg1:int):Object
		{
			var _local2:Object = this._npcBaseDataObj[_arg1];
			if (!_local2)
			{
				_local2 = this._npcBaseDataObj[NPC_DEFAULT_ID];
			};
			var _local3:int = _arg1;
			var _local4:String = _local2[NPC_BASE_NAME_INDEX];
			var _local5:int = parseInt(_local2[NPC_BASE_JOB_INDEX]);
			var _local6:String = this._npcJobObj[_local5][NPC_JOB_NAME_INDEX];
			var _local7:String = _local2[NPC_BASE_AVATAR_INDEX];
			var _local8:String = _local2[NPC_BASE_SKIN_INDEX];
			var _local9:Array = _local2[NPC_BASE_CONTENT_INDEX];
			var _local10:int = parseInt(_local2[NPC_BASE_TYPE_INDEX]);
			var _local11:int = parseInt(_local2[NPC_BASE_MAP_ID_INDEX]);
			var _local12:int = parseInt(_local2[NPC_BASE_ICON_INDEX]);
			var _local13:Boolean = (((_local2[NPC_BASE_CAN_SEARCH_WAY] == "1")) ? true : false);
			var _local14:String = _local2[NPC_BASE_SAYWORD];
			var _local15 = "";
			if (_local11 == 0)
			{
				_local15 = "未知的地图";
			}
			else
			{
				_local15 = WorldManager.getMapName(_local11);
			}
			if ((((_local6 == _local4)) || ((_local5 == 0))))
			{
				_local6 = "";
			}
			return ({
				"id":_arg1,
				"name":_local4,
				"jobID":_local5,
				"jobName":_local6,
				"avatar":("com/npcs/" + _local7),
				"skin":_local8,
				"content":_local9,
				"type":_local10,
				"mapID":_local11,
				"mapName":_local15,
				"canSearchWay":_local13,
				"sayWord":_local14
			});
		}
		
		public function getNPCActionArr(_arg1:int):Array
		{
			var _local5:int;
			var _local2:Object = this._npcBaseDataObj[_arg1];
			if (!_local2)
			{
				return ([]);
			};
			var _local3:Array = _local2[NPC_BASE_ACTION_INDEX];
			var _local4:Array = [];
			for each (_local5 in _local3)
			{
				if (!(((_local5 == 0)) || (!(this.authShowAction(this._npcActionObj[_local5])))))
				{
					_local4.push(this._npcActionObj[_local5]);
				};
			};
			return (_local4);
		}
		
		private function authShowAction(_arg1:Array):Boolean
		{
			if (!_arg1)
			{
				return (false);
			};
			var _local2:int = _arg1[0];
			if ((((_local2 == 61)) && ((GlobalObjectManager.getInstance().user.base.family_id > 0))))
			{
				return (false);
			};
			if (_local2 == 63)
			{
				if (GlobalObjectManager.getInstance().getFamilyID() > 0)
				{
					return (false);
				};
			};
			if (_local2 == 66)
			{
				return (false);
			};
			return (true);
		}
		
		
	}
}
